#include <iostream>
#include <chrono>
#include <type_traits>
using namespace std;
using namespace std::chrono;

// 编译期数值计算
template <unsigned int N>
struct Factorial 
{
    static const unsigned int value = N * Factorial<N - 1>::value;
};

// 终止条件特化
template <>
struct Factorial<0> 
{
    static const unsigned int value = 1;
};

// 运行时递归计算的阶乘函数
unsigned int factorial_recursive(unsigned int n) {
    if (n == 0) return 1;
    return n * factorial_recursive(n - 1);
}

// 运行时迭代计算的阶乘函数（通常更快）
unsigned int factorial_iterative(unsigned int n) {
    unsigned int result = 1;
    for (unsigned int i = 1; i <= n; ++i) {
        result *= i;
    }
    return result;
}

int main() {
    constexpr unsigned int n = 15; // 测试的阶乘数
    
    // 编译期计算
    auto start_compile = high_resolution_clock::now();
    constexpr unsigned int fact_compile = Factorial<n>::value;
    auto end_compile = high_resolution_clock::now();
    auto duration_compile = duration_cast<nanoseconds>(end_compile - start_compile);
    
    cout << "编译期计算:" << endl;
    cout << n << "的阶乘是: " << fact_compile << endl;
    cout << "编译期计算时间: " << duration_compile.count() << " 纳秒" << endl;
    cout << endl;
    
    // 运行时递归计算
    auto start_recursive = high_resolution_clock::now();
    unsigned int fact_recursive = factorial_recursive(n);
    auto end_recursive = high_resolution_clock::now();
    auto duration_recursive = duration_cast<nanoseconds>(end_recursive - start_recursive);
    
    cout << "运行时递归计算:" << endl;
    cout << n << "的阶乘是: " << fact_recursive << endl;
    cout << "运行时递归计算时间: " << duration_recursive.count() << " 纳秒" << endl;
    cout << endl;
    
    // 运行时迭代计算
    auto start_iterative = high_resolution_clock::now();
    unsigned int fact_iterative = factorial_iterative(n);
    auto end_iterative = high_resolution_clock::now();
    auto duration_iterative = duration_cast<nanoseconds>(end_iterative - start_iterative);
    
    cout << "运行时迭代计算:" << endl;
    cout << n << "的阶乘是: " << fact_iterative << endl;
    cout << "运行时迭代计算时间: " << duration_iterative.count() << " 纳秒" << endl;
    cout << endl;
    
    // 多次运行以获得更准确的时间测量（迭代版本）
    const int iterations = 1000000;
    auto start_iterative_multi = high_resolution_clock::now();
    for (int i = 0; i < iterations; ++i) {
        factorial_iterative(n);
    }
    auto end_iterative_multi = high_resolution_clock::now();
    auto duration_iterative_multi = duration_cast<microseconds>(end_iterative_multi - start_iterative_multi);
    
    cout << "多次运行测试 (" << iterations << " 次迭代):" << endl;
    cout << "总时间: " << duration_iterative_multi.count() << " 微秒" << endl;
    cout << "平均每次计算时间: " << static_cast<double>(duration_iterative_multi.count()) / iterations << " 微秒" << endl;
    cout << endl;
    
    // 验证结果是否正确
    cout << "结果验证:" << endl;
    cout << "编译期 == 递归: " << (fact_compile == fact_recursive ? "正确" : "错误") << endl;
    cout << "编译期 == 迭代: " << (fact_compile == fact_iterative ? "正确" : "错误") << endl;
    cout << "递归 == 迭代: " << (fact_recursive == fact_iterative ? "正确" : "错误") << endl;
    
    return 0;
}
